const http = require("../../api/index");
const { baseURL } = require("../../utils/request");

Page({
  data: {
    latitude: 0,
    longitude: 0,
    markers: [],
    show:false,
    campItem:{},
    oss_url:baseURL
  },
  //获取营地列表数据
  getCampList() {
    http.campList().then(res => {
      console.log(res, "获取营地列表数据");
      if (res.code === 200) {
        // 创建一个空数组用于存储处理后的数据
        const processedMarkers = res.data.map((item, index) => {
          return {
            id: index + 1,
            markerId: index + 1,
            campId: item.campId,
            iconPath: '../../images/static/point2.png',
            latitude: item.latitude,
            longitude: item.longitude,
            width: 30,
            height: 35,
            callout: {
              content: item.campName,
              display: 'ALWAYS',
            },
          };
        });
  
        console.log(processedMarkers, "processedMarkers");
        this.setData({
          markers: processedMarkers
        });
        console.log(this.data.markers);
      }
    });
  },
  /**
   * 进入页面获取当前位置
   * 调用接口 获取makers营地数组
  */
  onLoad(options) {
    // 检查用户权限状态
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.userLocation']) {
          // 用户尚未授权或已拒绝授权
          // 主动请求授权
          this.requestLocationAuthorization();
        } else {
          // 已经授权，直接获取位置信息
          this.getUserLocation();
        }
      }
    });
    //获取营地列表数据
    this.getCampList()
  },
  
  requestLocationAuthorization() {
    // 请求用户授权位置信息
    wx.authorize({
      scope: 'scope.userLocation',
      success: () => {
        this.getUserLocation();
      },
      fail: (err) => {
        console.error('授权失败:', err);
        // 如果用户拒绝了授权，可以进一步提示用户去设置中手动开启
        this.showSettingPrompt();
      }
    });
  },
  
  showSettingPrompt() {
    // 提示用户去设置中开启位置权限
    wx.showModal({
      title: '需要位置权限',
      content: '请在设置中开启位置权限以正常使用本功能。',
      showCancel: false,
      confirmText: '去设置',
      success: (res) => {
        if (res.confirm) {
          wx.openSetting({
            success: (settingRes) => {
              if (settingRes.authSetting['scope.userLocation']) {
                // 用户在设置中开启了位置权限
                this.getUserLocation();
              } else {
                // 用户仍然拒绝位置权限
                console.log('用户仍未开启位置权限');
              }
            },
          });
        }
      },
    });
  },
  getUserLocation() {
    wx.getLocation({
      // type: 'wgs84',
      type: 'gcj02',
      success: (res) => {
        // console.log(res,"返回的数据")
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: this.data.markers
        //   markers: [
        //     {
        //       id: 666,
        //       latitude: 38.1648964,
        //       longitude: 114.5851623,
        //       width: 30,
        //       height: 35,
        //       iconPath: '../../images/static/point2.png',
        //       callout: {
        //         content: '营地1',
        //         display: 'ALWAYS',
        //       }
        //     }
        // ]
        });
        
      },
      fail: (err) => {
        console.error('获取位置失败:', err);
      },
      complete: () => {
        console.log('位置获取操作已完成');
      }
    });
  },
  /**
   * 点击露营点信息
   * 弹出popup 并且获取到markId
  */
  tapPoint(e){
    // console.log(e,"eeeeeeeeee")
    const markId = e.detail.markerId
    // console.log(e,"获取点击点信息","标记点的id:",markId);
    const campId = this.findCampId(markId)
    console.log(campId,"campId")
    http.campDetail(campId).then(res=>{
      if(res.code == 200) {
        this.setData({
          campItem:res.data,
          show:true
        })
      }
      console.log(this.data.oss_url+res.data.imageUrl,"获取到营地详情")
    })
  },
  /**
   * 写一个方法用来查找campId
   * 首先方法接收一个参数markerId 
   * 然后遍历markers数组，如果id 或者 markerId == markerId 那就把改对象对应的campId 返回
   */
  findCampId(markId){
    const foundItem = this.data.markers.find(item => item.id === markId);
    if (foundItem) {
      console.log(foundItem.campId,"campId");
      return foundItem.campId;
    }
    return undefined; // 如果没有找到，返回undefined
  },
  //关闭弹出层
  onClose() {
    this.setData({ show: false });
  },
  // 点击底部搜索框
  goFindCamp(){
    wx.navigateTo({
      url: '/pages/findCamp/findCamp',
    });
  },
  //点击查看详情
  goCampDetail(e){
    console.log(e,"22222222222");
    const campId = e.target.dataset.campId
    //跳转到营地详情页
    wx.navigateTo({
      url: `/pages/detail/detail?campId=${campId}`,
    });
  }

});